{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b79d94a7-82d4-449a-a78a-b3a6f6f95ed0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-19T08:18:54.951112Z",
     "iopub.status.busy": "2022-05-19T08:18:54.950729Z",
     "iopub.status.idle": "2022-05-19T08:18:55.285635Z",
     "shell.execute_reply": "2022-05-19T08:18:55.284779Z",
     "shell.execute_reply.started": "2022-05-19T08:18:54.951032Z"
    }
   },
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0ac54a04-932d-46b4-adb8-d88a84480e60",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-19T08:21:51.869505Z",
     "iopub.status.busy": "2022-05-19T08:21:51.869072Z",
     "iopub.status.idle": "2022-05-19T08:21:51.883289Z",
     "shell.execute_reply": "2022-05-19T08:21:51.882611Z",
     "shell.execute_reply.started": "2022-05-19T08:21:51.869477Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       2\n",
       "1   -8976\n",
       "2       1\n",
       "3   -9999\n",
       "4       4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据对象\n",
    "data = pd.Series([2, -8976, 1, -9999, 4])\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95d6db63-67b3-461b-a4e6-226bb1b03299",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "## 一、replace-参数形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "145b5753-f902-4218-b0bc-27047d0f7d96",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-19T08:30:11.401464Z",
     "iopub.status.busy": "2022-05-19T08:30:11.401059Z",
     "iopub.status.idle": "2022-05-19T08:30:11.410962Z",
     "shell.execute_reply": "2022-05-19T08:30:11.409958Z",
     "shell.execute_reply.started": "2022-05-19T08:30:11.401392Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       2.0\n",
       "1   -8976.0\n",
       "2       1.0\n",
       "3       NaN\n",
       "4       4.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用nan替换掉-9999\n",
    "data.replace(-9999, np.nan)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a7fde126-b61e-48d3-872a-90d6ebf70fcb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-19T08:35:55.979821Z",
     "iopub.status.busy": "2022-05-19T08:35:55.979374Z",
     "iopub.status.idle": "2022-05-19T08:35:55.988997Z",
     "shell.execute_reply": "2022-05-19T08:35:55.988158Z",
     "shell.execute_reply.started": "2022-05-19T08:35:55.979794Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    2.0\n",
       "1    NaN\n",
       "2    1.0\n",
       "3    NaN\n",
       "4    4.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用nan替换掉-9999和-8976\n",
    "data.replace([-9999, -8976], np.nan)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dd415051-1344-481a-b745-063661c9f545",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-19T08:44:11.751803Z",
     "iopub.status.busy": "2022-05-19T08:44:11.751274Z",
     "iopub.status.idle": "2022-05-19T08:44:11.759508Z",
     "shell.execute_reply": "2022-05-19T08:44:11.758374Z",
     "shell.execute_reply.started": "2022-05-19T08:44:11.751776Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    2.0\n",
       "1    0.0\n",
       "2    1.0\n",
       "3    NaN\n",
       "4    4.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用nan替换掉-9999，用0替换掉-8976\n",
    "data.replace([-9999, -8976], [np.nan, 0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed273e8f-89ca-4a0a-8995-7baa8ebed2e5",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "## 二、replace-字典形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d4e5b896-2a6e-4f27-af4b-871a30a54f68",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-19T09:04:31.595676Z",
     "iopub.status.busy": "2022-05-19T09:04:31.595218Z",
     "iopub.status.idle": "2022-05-19T09:04:31.603363Z",
     "shell.execute_reply": "2022-05-19T09:04:31.602639Z",
     "shell.execute_reply.started": "2022-05-19T09:04:31.595649Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    2.0\n",
       "1    0.0\n",
       "2    1.0\n",
       "3    NaN\n",
       "4    4.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用nan替换掉-9999，用0替换掉-8976，字典形式\n",
    "data.replace({-9999: np.nan, -8976: 0})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
